package com.example.blogserver.service.Impl;

import com.example.blogserver.mapper.UserMapper;
import com.example.blogserver.pojo.User;
import com.example.blogserver.service.LoginService;
import com.example.blogserver.a_utils.Constant;
import com.example.blogserver.a_utils.ImageUtil;
import com.example.blogserver.a_utils.ResponseBodyMessage;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;

@SuppressWarnings({"all"})
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    public ResponseBodyMessage<Boolean> checkOut(HttpServletRequest request,
                                                 String account,
                                                 String password,
                                                 String verifyCode) {
        //创建响应body
        ResponseBodyMessage responseBodyMessage = new ResponseBodyMessage();
        //统一给定为false-》验证通过后给定为true
        responseBodyMessage.setStatus(-1);
        responseBodyMessage.setData(false);
        //获取session会话
        HttpSession session = request.getSession(false);
        String checkCode = (String) session.getAttribute("verifyCode");
        if (!checkCode.equals(verifyCode)){
            responseBodyMessage.setMessage("验证码输入有误，请重新输入~");
            return responseBodyMessage;
        }
        User user = userMapper.queryUser(account);
        if (user == null){
            responseBodyMessage.setMessage("用户不存在，请检查账号是否正确~");
            return responseBodyMessage;
        }
        Boolean ret = bCryptPasswordEncoder.matches(password,user.getPassword());
        if (!ret){
            responseBodyMessage.setMessage("账号或密码错误，请检查后重新登录~");
            return responseBodyMessage;
        }
        //走到这一步说明验证通过
        responseBodyMessage.setStatus(1);
        responseBodyMessage.setMessage("登录成功~");
        responseBodyMessage.setData(true);
        //将用户信息存储在session会话当中
        session.setAttribute(Constant.USERINFO_SESSION_KEY,account);
        //移除Session当中存储的图形验证码
        session.removeAttribute("verifyCode");
        return responseBodyMessage;
    }

    @Override
    @SneakyThrows
    public void buildCheckImage(HttpServletRequest request, HttpServletResponse response) {
        final ImageUtil imageUtil = ImageUtil.getInstance();
        final ByteArrayInputStream image = imageUtil.getImage();
        //验证码图片
        final String verifyCode = imageUtil.getStr();
        //验证码文字
        HttpSession session = request.getSession(true);//如果没有session，则允许创建
        session.setAttribute("verifyCode",verifyCode);
        response.setContentType("image/jpeg");

        byte[] bytes = new byte[1024];
        try (final ServletOutputStream out = response.getOutputStream()){
            while (image.read(bytes) != -1){
                out.write(bytes);
            }
        }
    }

    @Override
    public ResponseBodyMessage<Boolean> loginOut(HttpServletRequest request) {
        //移除session中的会话字段
        HttpSession session = request.getSession(false);
        session.removeAttribute(Constant.USERINFO_SESSION_KEY);
        request.getSession().invalidate();
        //返回响应
        ResponseBodyMessage result = new ResponseBodyMessage();
        result.setStatus(1);
        result.setMessage("退出登录~");
        result.setData(true);
        return result;
    }
}
